<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;

/**
 * @property int $id
 * @property string $label
 * @property string $key
 * @property string $value
 * @property string $type
 * @property string $remark
 * @property string $group
 * @property Carbon $created_at
 * @property Carbon $updated_at
 */
class SystemConfig extends Model
{
    protected $guarded = [];

    public static function getValue($key, $default = null)
    {
        return cache()->rememberForever('system_config:'.$key, function () use ($key, $default) {
            return optional(self::where('key', $key)->first())->value ?? $default;
        });
    }

    // 清除配置缓存
    public static function clearCache($key = null): void
    {
        if ($key) {
            Cache::forget('system_config:'.$key);
        } else {
            Cache::flush();
        }
    }
}
